# vim: set ft=zsh :

# Copy user's public key to the clipboard. If there is not
# one, prompt the user whether she wants to create one

local key=$HOME/.ssh/id_ed25519.pub

if [ ! -f $key ]; then
    echo "This user seems not to have an SSH public key"
    read -q "?Would you like to create one? [y/n]: "

    if [ $? -eq 0 ]; then
        echo
        ssh-keygen -t ed25519 -f ${key:r}
    else
        return $?
    fi
fi

# Determine which command is available for copying to the clipboard.
# If there's none, the key will be echoed to stdout
if exists pbcopy > /dev/null 2>&1; then
    cat "$key" | pbcopy
elif exists xsel; then
    cat "$key" | xsel -ib
else
    echo -n "ssh-key: you don't have any utility which you can access the clipboard from. "
    echo "Your key will be echoed to stdout..."
    echo

    cat "$key"

    return 0
fi

echo "Your SSH public key's been copied to clipboard!"
